package ai.people.platform.system.service.impl;

import ai.people.netmon.framework.domain.system.entity.NmSystemUser;
import ai.people.netmon.framework.domain.system.entity.NmSystemUserInfo;
import ai.people.netmon.framework.domain.system.request.SystemUserRequest;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.platform.system.mapper.SystemUserInfoMapper;
import ai.people.platform.system.service.SystemUserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统用户信息服务impl
 *
 * @author yuanqinglong
 * @date 2022/3/22 13:58
 */
@Service
public class SystemUserInfoServiceImpl implements SystemUserInfoService {


    @Autowired
    private SystemUserInfoMapper systemUserInfoMapper;


    /**
     * 通过账号查询用户信息
     *
     * @param account 账户
     * @return {@link NmSystemUserInfo}
     */
    @Override
    public NmSystemUserInfo getUserInfoByAccount(String account) {
        NmSystemUserInfo nmSystemUserInfo = systemUserInfoMapper.selectOne(Wrappers.lambdaQuery(NmSystemUserInfo.class).eq(NmSystemUserInfo::getAccount, account));
        AssertUtils.isNotNull(nmSystemUserInfo, CommonEnum.QUERY_NOT_FOUND);
        return nmSystemUserInfo;
    }

    /**
     * 通过id查询用户信息
     *
     * @param userId 用户id
     * @return {@link NmSystemUserInfo}
     */
    @Override
    public NmSystemUserInfo getUserById(Long userId) {
        NmSystemUserInfo nmSystemUserInfo = systemUserInfoMapper.selectById(userId);
        AssertUtils.isNotNull(nmSystemUserInfo, CommonEnum.QUERY_NOT_FOUND);
        return nmSystemUserInfo;
    }

    /**
     * 用户信息列表
     *
     * @param systemUserRequest 系统用户请求
     * @return {@link PageResponse}<{@link NmSystemUserInfo}>
     */
    @Override
    public PageResponse<NmSystemUserInfo> listUsersInfo(SystemUserRequest systemUserRequest) {
        IPage<NmSystemUserInfo> page = new Page<>(systemUserRequest.getOriginPageNo(),systemUserRequest.getPageSize());
        LambdaQueryWrapper<NmSystemUserInfo> lambdaQuery = Wrappers.lambdaQuery(NmSystemUserInfo.class);
        IPage<NmSystemUserInfo> selectPage = systemUserInfoMapper.selectPage(page, lambdaQuery);
        return PageResponse.builder(selectPage);
    }

    /**
     * 添加系统用户信息
     *
     * @param nmSystemUserInfo 系统用户信息
     */
    @Override
    public void addSystemUserInfo(NmSystemUserInfo nmSystemUserInfo) {
        int insertResult = systemUserInfoMapper.insert(nmSystemUserInfo);
        AssertUtils.isTrue(insertResult > 0, CommonEnum.FAIL);
    }

    /**
     * 更新用户
     *
     * @param nmSystemUserInfo 系统用户
     */
    @Override
    public void updateUserInfo(NmSystemUserInfo nmSystemUserInfo) {
        int updateResult = systemUserInfoMapper.updateById(nmSystemUserInfo);
        AssertUtils.isTrue(updateResult > 0, CommonEnum.FAIL);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户id
     */
    @Override
    public void deleteUserInfo(Long userId) {
        int deleteResult = systemUserInfoMapper.deleteById(userId);
        AssertUtils.isTrue(deleteResult > 0, CommonEnum.FAIL);
    }
}
